package cn.fsw.pethome.org.controller;

import cn.fsw.pethome.annotations.PreAuthorize;
import cn.fsw.pethome.org.domain.Systemdictionary;
import cn.fsw.pethome.org.query.SystemdictionaryQuery;
import cn.fsw.pethome.org.service.ISystemdictionaryService;
import cn.fsw.pethome.util.AjaxResult;
import cn.fsw.pethome.util.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system")
@Api(value = "系统词典的API",description="系统词典相关的CRUD功能")
public class SystemdictionaryController {
    @Autowired
    private ISystemdictionaryService systemdictionaryService;

    @GetMapping("/list")
    @ApiOperation(value = "查询全部系统词典信息" )
    public List<Systemdictionary> selectAll(){
        return systemdictionaryService.selectAll();
    }


    @PreAuthorize(name = "系统词典列表",value = "system:pagelist")
    @PostMapping("/pageList")
    @ApiOperation(value = "高级查询和分页" )
    public PageList<Systemdictionary> pageSelevtAll(@RequestBody SystemdictionaryQuery query){

        return systemdictionaryService.pageSelevtAll(query);
    }

    @GetMapping("/list/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Systemdictionary selectById(@PathVariable("id") Long id){
        return null;
    }


    @PutMapping("/systemdictionary")
    @ApiOperation(value = "新增或者修改系统词典信息" )
    public AjaxResult update(@RequestBody Systemdictionary systemdictionary){
        try {
            if(systemdictionary.getId() == null){
                systemdictionaryService.add(systemdictionary);
            }else {
                systemdictionaryService.update(systemdictionary);
            }
            return AjaxResult.ajaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @DeleteMapping("/systemdictionary/{id}")
    @ApiOperation(value = "通过ID删除" )
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            systemdictionaryService.delete(id);
            return AjaxResult.ajaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
}
